#!/usr/bin/env python
# -*- coding:utf-8 -*-

"""
@author zyx
@since 2022/2/18 08:42
@file: m3u8_1905.py
"""
import requests
from urllib.parse import urljoin

headers = {
    'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) '
                  'Chrome/98.0.4758.102 Safari/537.36'
}
# 爬取1905电影视频
m3u8_url = 'https://m3u8i.vodfile.m1905.com/202202190027/5b34abb9e548d649e16a1819f483af96' \
           '/movie/2019/03/28/m201903286TO0ZJYJNYCKNTNY/E6EA61B864A09818E3F40C9DC.m3u8'

m3u8_text = requests.get(url=m3u8_url, headers=headers).text
m3u8_text = m3u8_text.strip()
# print(m3u8_text)

ts_url_list = [urljoin(m3u8_url, line) for line in m3u8_text.split('\n') if not line.startswith('#')]

for ts_url in ts_url_list:
    dir_name = 'tsfiles'
    file_name = ts_url.split('/')[-1]
    ts_path = dir_name + '/' + file_name
    ts_data = requests.get(url=ts_url, headers=headers).content
    with open(ts_path, 'wb') as fp:
        fp.write(ts_data)
    print(f'{file_name}下载成功')
